﻿using Microsoft.AspNetCore.Mvc;
using Qy.Scenario;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Home.Controllers;

[Area("Home")]
public class InfoController : Base
{
    private readonly ContentService _contentService;
    private readonly ContentCateService _contentCateService;
    private readonly AttmentService _attment;
    public InfoController(ContentService contentService, ContentCateService contentCateService, AttmentService attment)
    {
        _contentService = contentService;
        _contentCateService = contentCateService;
        _attment = attment;
    }

    /// <summary>
    /// 根据ID查询详细内容
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<IActionResult> IndexAsync(int id = 0)
    {
        if (id < 1) return View("../Common/404"); 
        ViewContentAndContent article = await _contentService.GetOneAsync(id);
        if (article == null) return View("../Common/404");
        ContentCate cateInfo = await _contentCateService.GetOneAsync(article.Cid);
        if (!string.IsNullOrEmpty(article.Attments))
        {
            List<int> attIds = JsonSerializer.Deserialize<List<int>>(article.Attments);
            ViewBag.attments = !string.IsNullOrEmpty(article.Attments) ? await _attment.GetListAsync(attIds) : null;
        }
        ViewBag.info = article;
        ViewBag.cateInfo = cateInfo;
        return View(article);
    }

}
